package com.lagou.edu.mvcframework.pojo;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * @author zx
 * @date 2021/7/26 21:58
 * @description
 */
public class Handler {

    private Object controller;// 记录对象

    private Method method;//反射获取方法

    private Pattern pattern;// 正则对象

    private Map<String, Integer> paramIndexMapping;// 记录参数顺序 进行参数绑定<参数名,第几个>

    private Set<String> permissions;// 记录有访问该url权限的用户列表

    public Handler(Object controller, Method method, Pattern pattern) {
        this.controller = controller;
        this.method = method;
        this.pattern = pattern;
        this.paramIndexMapping = new HashMap<>();
        this.permissions = new HashSet<>();
    }

    // 判断是否有权限访问
    public Boolean isAllowAccess(String username) {
        // 空代表不受权限控制
        if (permissions.isEmpty()) return true;

        // 受权限管控默认不能访问 有权限改为true
        Boolean result = Boolean.FALSE;
        if (permissions.contains(username)) {
            result = Boolean.TRUE;
        }
        return result;
    }

    public Object getController() {
        return controller;
    }

    public void setController(Object controller) {
        this.controller = controller;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Pattern getPattern() {
        return pattern;
    }

    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
    }

    public Map<String, Integer> getParamIndexMapping() {
        return paramIndexMapping;
    }

    public void setParamIndexMapping(Map<String, Integer> paramIndexMapping) {
        this.paramIndexMapping = paramIndexMapping;
    }

    public Set<String> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<String> permissions) {
        this.permissions = permissions;
    }
}

